package com.summer.order.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.summer.core.annotations.ScopeType;
import com.summer.core.controller.EntityQuery;
import com.summer.order.model.Cookbook;
import com.summer.order.model.OrderDetail;
import com.summer.user.model.User;

@Component
@Scope(ScopeType.VIEW)
public class OrderDetailList extends EntityQuery<OrderDetail> {

	private final String HQL = "select orderDetail from OrderDetail orderDetail";
	private final String COUNT_HQL = "select count(orderDetail) from OrderDetail orderDetail";

	private String cookbook_id;

	private Date startDate;

	private Date endDate;
	
	private String lunchDay;

	public OrderDetailList() {

		setCountHql(COUNT_HQL);
		setHql(HQL);
		final String[] RESTRICTIONS = {

				"orderDetail.user.id=#{orderDetailList.example.user.id}",
				"orderDetail.user.realname=#{orderDetailList.example.user.realname}",
				"orderDetail.cookbook.id like #{orderDetailList.cookbook_id}"

		};
		setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
	}

	@Override
	protected void initExample() {
		OrderDetail example = new OrderDetail();
		Cookbook cookbook = new Cookbook();
		User user = new User();
		example.setUser(user);
		example.setCookbook(cookbook);
		setExample(example);
	}


	public String getCookbook_id() {

		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		if (startDate != null && endDate != null) {
			cookbook_id = df.format(startDate) +  df.format(endDate);
		}
		return cookbook_id;
	}

	public void setCookbook_id(String cookbook_id) {
		this.cookbook_id = cookbook_id;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getLunch(String id,long i) throws ParseException {
		lunchDay = "空";
		OrderDetail od = getHt().get(OrderDetail.class, id);
		String lunchDetail = od.getLunchDetail();
		String breakfastDetail = od.getBreakfastDetail();
		String cookbookId =  od.getCookbook().getId();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date date = sdf.parse(cookbookId.substring(0,8));
		String[] lds = lunchDetail.split(",");
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, (int)i);
		date = cal.getTime();
		for(String ld :lds){
			if(ld.equals(sdf.format(date))){
				lunchDay = ld;
			}
		}
		return lunchDay;
	}
	
	public String getDetail(String id,long i) throws ParseException {
		lunchDay = "空";
		StringBuffer detail = new StringBuffer();
		OrderDetail od = getHt().get(OrderDetail.class, id);
		String lunchDetail = od.getLunchDetail();
		String breakfastDetail = od.getBreakfastDetail();
		String cookbookId =  od.getCookbook().getId();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date date = sdf.parse(cookbookId.substring(0,8));
		String[] lds = lunchDetail.split(",");
		String[] bds = breakfastDetail.split(",");
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, (int)i);
		date = cal.getTime();
		for(String bd :bds) {
			if(bd.equals(sdf.format(date))){
				detail.append(bd+"：早");
			}
		}
		for(String ld :lds){
			if(ld.equals(sdf.format(date))){
				if(!detail.toString().equals("")) {
					detail.append("+午");
				}else {
					detail.append(ld+"：午");
				}
			}
		}
		return detail.toString().equals("")?"空":detail.toString();
	}

}
